package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Order;
import csu.web.mypetstore.service.OrderService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

public class ViewOrderByOrderIdServlet extends HttpServlet {
    public static final String VIEW_ORDER = "/WEB-INF/jsp/order/viewOrderForm.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int orderId = Integer.parseInt(req.getParameter("orderID"));
        OrderService orderService = new OrderService();
        Order order = orderService.getOrderById(orderId);
        HttpSession httpSession= req.getSession();
        httpSession.setAttribute("order",order);
        req.getRequestDispatcher(VIEW_ORDER).forward(req,resp);
    }
}
